<?php


namespace App\Zesow\Cache\Impl;

use Illuminate\Support\Facades\Cache;
use App\Services\Business\Interfaces\CategoryServiceInterface;

class CateogryCache
{

    public const KEY_NAME = 'category';

    private $categoryService;

    public function __construct(CategoryServiceInterface $categoryService)
    {
        $this->categoryService = $categoryService;
    }

    public function tree()
    {

        if (Cache::has(self::KEY_NAME)) {
            return Cache::get(self::KEY_NAME);
        }
        $categories = $this->categoryService->tree();
        Cache::put(self::KEY_NAME, $categories);
        return $categories;


    }

    public function destroy(): void
    {
        Cache::forget($this->key());
    }

    private function key(): string
    {
        return self::KEY_NAME ;
    }

}
